# -*- encoding:utf-8 -*-
# @Time: 2023/1/1 16:14
# @Author :xibin
# @Email: xibin@maqu. com
# @File: serializers.py
# @Software: PyCharm
from rest_framework import serializers
from rest_framework.serializers import ModelSerializer

from shopping.models import *


class ClassificationSerializer(ModelSerializer):
    class Meta:
        model = Classification
        # exclude = ['is_delete']
        fields = ['id','name']

class ParentClassificationSerializer(ModelSerializer):
    classification_set = ClassificationSerializer(many=True,read_only=True)
    class Meta:
        model = Classification
        fields = ['id', 'name','classification_set','parent']

    extra_kwargs = {
        'parent':{'write_only':True}
    }

class CommodityImgSerializer(ModelSerializer):
    class Meta:
        model = CommodityImg
        fields = ['src']

class CommoditySerializer(ModelSerializer):
    classification1_name = serializers.CharField(source='classification1.name',read_only=True)
    classification2_name = serializers.CharField(source='classification2.name',read_only=True)
    commodity_set = CommodityImgSerializer(many=True,read_only=True)

    class Meta:
        model = Commodity
        exclude = ['is_delete']




